#!/usr/bin/env python
#-*- coding:utf-8 -*-

from common.node import Node
from graph.node_graph import NodeGraph

class NodeTree(NodeGraph):
    def __init__(self, root=None, nodes=None, edges=None):
        super(NodeTree, self).__init__(nodes, edges)
        self.root = root

    @property
    def root(self):
        if hasattr(self, "_root"):
            return self._root
        return None

    @root.setter
    def root(self, root):
        if isinstance(root, Node):
            self._root = root.nid
        else:
            self._root = root

    def add_root(self, root):
        if isinstance(root, Node):
            self.add_node(root)
            self.root = root
        else:
            raise Exception("root should be instance of Node!")

    @property
    def root_node(self):
        return self.node(self.root)